package com.aruba.filter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * 色彩矩阵ColorMatrix
 */
public class ColorMatrixView extends View {
    private Bitmap bitmap;
    private int progress;
    private  float saturation;
    private float rotate;

    public ColorMatrixView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);

        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.xyjy2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //画原图
        Paint paint = new Paint();
        RectF rectF = new RectF(100, 100, 100 + bitmap.getWidth() / 2, 100 + bitmap.getHeight() / 2);
        canvas.drawBitmap(bitmap, null, rectF, paint);

        //画设置ColorFilter效果后的图
        rectF = new RectF(600, 100, 600 + bitmap.getWidth() / 2, 100 + bitmap.getHeight() / 2);
        ColorMatrix colorMatrix = new ColorMatrix();
        //和颜色增强效果相同
//        colorMatrix.setScale(1.2f, 1.2f, 1.2f, 1);
//        colorMatrix.setSaturation(saturation);
        //aixs-- 0 红色轴，1，绿色，2，蓝色
        // degrees -- 旋转的角度
        colorMatrix.setRotate(0,rotate);
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        canvas.drawBitmap(bitmap, null, rectF, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                saturation+=20f;
                rotate +=40f;
                invalidate();
                break;
        }

        return true;
    }

}
